#include <iostream>
#include <string>
#include "main.hpp"
#include "SocketTCP.hpp"

using namespace std;
using namespace vrts;

int main(int argc, char** argv)
{
    TacviewDisplayInfo m_displayInfo = { "nVyrtuoz", "Frantz RAIA", "DHTJ", "LinkView001", "VRTS_Sim", "LinkView" };

    SocketTCP tacviewServer;
    // 1. 建立TCP链接
    tacviewServer.Bind(1316, NULL);
    tacviewServer.Accept();

     // 2.发送host握手消息
    std::string strHandShakeRequest =
        "XtraLib.Stream.0\nTacview.RealTimeTelemetry.0\n" + m_displayInfo.hostName + "\n\x00";
    tacviewServer.SendData((char*)strHandShakeRequest.data(), strHandShakeRequest.size()+1);// send the last '\x00'
    
    // 3. 接收握手消息
    char recvBuf[1024] = {0};
    int recvLen = 1024;
    while(true)
    {
        tacviewServer.ReceiveData(recvBuf, recvLen);
       if(recvLen > 0) 
       {
        std::cout << recvLen << std::endl;
        std::string strExpectHandShakeResp =
                    "XtraLib.Stream.0\nTacview.RealTimeTelemetry.0\n" + m_displayInfo.clientName + "\n0\x00";
        std::string shakeResp = recvBuf;
        std::cout << shakeResp.c_str() << std::endl;
        if(shakeResp == strExpectHandShakeResp)
            break;
       }
        Sleep(100);
    }

    // 4. 发送元数据
    time_t rawtime;
    struct tm *timeinfo;
    char szDatatime[100] = { 0 };
    time( &rawtime );
    rawtime -= 28800;
    timeinfo    = localtime( &rawtime );
    time_t tick = mktime( timeinfo );

    strftime( szDatatime, sizeof( szDatatime ), "%Y-%m-%dT%H:%M:%SZ", timeinfo );
    std::string strDateTime = szDatatime;

    std::string strReferenceTime = "0,ReferenceTime=" + strDateTime + "\n";
    std::string strRecordingTime = "0,RecordingTime=" + strDateTime + "\n";
    std::string strTitle         = "0,Title=" + m_displayInfo.title + "\n ";
    std::string strDataRecorder  = "0,DataRecorder=" + m_displayInfo.dataRecorder + "\n ";
    std::string strDataSource    = "0,DataSource=" + m_displayInfo.dataSource + "\n ";
    std::string strAuthor        = "0,Author=" + m_displayInfo.author + "\n";
    std::string strLongitude     = "0,ReferenceLongitude=0\n";
    std::string strLatitude      = "0,ReferenceLatitude=0\n";

    std::string strHeader   = "FileType=text/acmi/tacview\nFileVersion=2.2\n";
    std::string strSendData = strHeader + strReferenceTime + strRecordingTime + strTitle + strDataRecorder +
                                strDataSource + strAuthor + strLongitude + strLatitude;
    tacviewServer.SendData((char*)strSendData.data(), strSendData.size());

    // 5. 发送有效数据
    // 5.1 发送波赛点数据
    /* id,T=longitude|latitude|altitude|*/
    std::string bullseye= "40000001,T=120.117208|23.274011|2000|617414|-291014,Type=Navaid+Bullseye,Color=Blue,Coalition=Enemies\n";
    tacviewServer.SendData((char*)bullseye.data(), bullseye.size());
    bullseye="40000002,T=121.117208|23.274011|2000|371700|11557,Type=Navaid+Bullseye,Color=Red,Coalition=Allies\n";
    tacviewServer.SendData((char*)bullseye.data(), bullseye.size());

    // 5.2 发送飞机数据

    /*数据精度要求如下：
    * Property                 Suggested resolution
    * #frame time                  0.01 second
    * T(longitude/latitude)        0.0000001 degree
    * T(altitude)                  0.01 meter
    * T(roll/pitch/yaw)            0.1 degree
    * T(flat coordinates)          0.01 meter
    * AGL                          0.01 meter
    * HDG                          0.1 degree
    * IAS                          0.1 m/s
    */
    std::string platcreateInfo = ",Type=Air+FixedWing,Name=C-130,Color=Red,Coalition=Allies,Country=cn,Pilot=#1\n";
    std::string platInfo = "#0\n1,T=120.117208|23.274011|2000|0|0|0" +platcreateInfo;//#clock\nid,T=longitude|latitude|altitude|rollDegree|pitch|heading
    tacviewServer.SendData((char*)platInfo.data(), platInfo.size());


    int sec = 0;
    double latDeg = 23.274011;
    while(true)
    {
        latDeg += 0.003;
        std::string time = std::to_string(sec++); 
        std::string latitude = std::to_string(latDeg);
        std::string platInfo = "#" + time + "\n1,T=120.117208|" +  latitude +"|2000|0|0|0\n"; 
        tacviewServer.SendData((char*)platInfo.data(), platInfo.size());

        cout << "flying" << endl;
        Sleep(1000); 
    }
    return 0;
}